from flask import render_template, abort
from flask import flash, request, current_app
from flask.helpers import url_for
from flask_login import current_user
from werkzeug.utils import redirect

from . import main, forms
from app import models, tools
from app import db


# 首页
@main.route("/")
def index():
    return render_template("index.html")

# 文章列表页
@main.route("/articles")
def articles():
    page = request.args.get("page", 1, int)
    limit = current_app.config.get("DEFAULT_PAGE_NUMBER", 10)
    offset = (page-1)*limit

    pages = models.Article.query.filter_by(deleted="false").count() / current_app.config.get("DEFAULT_PAGE_NUMBER", 10)
    if str(pages).split(".")[-1] != "0":
        pages = int(pages)+1
    pages = int(pages)

    posts = models.Article.query.filter_by(deleted="false")\
        .order_by(models.Article.updated_time.desc()).offset(offset).limit(limit).all()

    return render_template("articles.html", posts=posts, pages=pages, page=page)

# 关于页
@main.route("/about")
def about():
    
    return render_template("about.html")

# 文章单页  uid:用户id；aid:文章id
@main.route("/article/<int:uid>/<int:aid>")
def user_article_page(uid, aid):
    article = models.Article.query.filter_by(id=aid, user_id=uid, deleted="false").first()
    if not article:
        return abort(404)
    
    return render_template("article_page.html", article=article)

# 图片列表页
@main.route("/images")
def images():
    page = request.args.get("page", 1, int)
    limit = current_app.config.get("DEFAULT_PAGE_NUMBER", 10)
    offset = (page-1)*limit

    pages = models.Images.query.filter_by(deleted="false").count() / current_app.config.get("DEFAULT_PAGE_NUMBER", 10)
    if str(pages).split(".")[-1] != "0":
        pages = int(pages)+1
    pages = int(pages)

    posts = models.Images.query.filter_by(deleted="false")\
        .order_by(models.Images.created_time.desc()).offset(offset).limit(limit).all()
    return render_template("images.html", posts=posts, pages=pages, page=page)

# 用户单页  uid:用户id
@main.route("user_page/<int:uid>", methods=["GET", "POST"])
def user_page(uid):

    user = models.User.query.filter_by(id=uid, deleted="false").first()
    if not user:
        return abort(404)

    articles, page = tools.get_user(models.Article, user)
    pages = tools.get_page_all(models.Article, user)

    form = forms.UserFollowersForm(user)
    if request.method == "POST" :
        if current_user.is_authenticated and form.followed.data or form.follower.data:
            if current_user.id == user.id:
                flash("不能关注自己", "info")
                return redirect(request.path)
            if not user.is_following(current_user):
                user.follow(current_user)
                flash("关注成功", "success")
            else:
                user.unfollow(current_user)
                flash("取关成功", "success")
            return redirect(url_for("main.user_page", uid=uid))
        else:
            flash("登录才能加关注", "info")
            return redirect(url_for("auth.login"))        
    return render_template("user_page.html",form=form, user=user, articles=articles, page=page, pages=pages)